/*====================================================================================
                                                                     
		          O-Ring Production Networks
			        By Demir-Fieler-Xu-Yang
  
*====================================================================================*/	

clear all
set more off
log close _all
set matsize 5000

cd "\data\" // Add your directory here

/***************************************
/Table 5
****************************************/

use manufsample_2015.dta,clear

foreach y of varlist indegree outdegree sales_b {
	replace `y'=ln(`y')
}

egen s=group(nace4_b  )

reg outdegree sales_b  , vce(cluster s)
estimates store col1
estadd local "FE" "" , replace

reghdfe outdegree sales_b  ,absorb(s) vce(cluster s)
estimates store col2
estadd local "FE" "s" , replace

reghdfe outdegree sales_b avgwage_adj_b ,absorb(s) vce(cluster s)
estimates store col3
estadd local "FE" "s" , replace

reg indegree sales_b  , vce(cluster s)
estimates store col4
estadd local "FE" "" , replace

reghdfe indegree sales_b  ,absorb(s) vce(cluster s)
estimates store col5
estadd local "FE" "s" , replace

reghdfe indegree sales_b avgwage_adj_b ,absorb(s) vce(cluster s)
estimates store col6
estadd local "FE" "s" , replace


noi esttab col1 col2 col3 col4 col5 col6, replace compress width(1\hsize)  se  s(N r2 FE clustvar)

/***************************************
/Table 7 (Data moments)
****************************************/
// Identify firms in the main sample

use manufsample_2015.dta,clear

keep buyer_ID
rename buyer_ID ID
gen insample=1
sort ID
save temp3.dta,replace

// Construct wage quintiles

use temp2,clear

keep if year==2015

destring ID,replace
sort ID
merge ID using temp3
keep if insample==1
drop _m

sort avgwage_adj
xtile wageq = avgwage_adj, nq(5) 
collapse (mean) wageq,by(ID)

destring ID,replace
sort ID
save temp3.dta,replace


//Construct moments

* Share of network sales

use masterdata.dta,clear

keep if year==2015

keep if nace2_b <34 // Keep manufacturing buyers
keep if nace2_s <34 // Keep manufacturing suppliers
drop if avgwage_adj_b==0 | avgwage_adj_b==.
drop if avgwage_adj_s==0 | avgwage_adj_s==.

gen ID=buyer_ID
destring ID,replace 
sort ID
merge ID using temp3.dta
keep if _m==3
drop _m 

gen ID=seller_ID
destring ID,replace 
sort ID
merge ID using temp3.dta
keep if _m==3
drop _m wageq 


rename value value_network

collapse (sum) value_network,by(ID)

destring ID,replace
sort ID
save temp4.dta,replace

* Export and import intensity

use masterdata.dta,clear

keep if year==2015

drop if avgwage_adj_b==0 | avgwage_adj_b==.
drop if avgwage_adj_s==0 | avgwage_adj_s==.

preserve

collapse (sum) value,by(seller_ID)
rename value domsalesvalue
rename seller_ID ID
destring ID,replace 
sort ID
save temp5.dta,replace


restore
collapse (sum) value,by(buyer_ID)
rename value dompurchvalue

rename buyer_ID ID
destring ID,replace 
sort ID
merge ID using temp3.dta
keep if _m==3
drop _m 

sort ID
merge ID using temp5.dta
drop if _m==2
drop _m

sort ID
save temp5.dta,replace

*Load master data in 2015

use manufsample_2015.dta,clear

rename buyer_ID ID
destring ID,replace
sort ID
merge ID using temp3.dta,replace
keep if _m==3
drop _m
capture erase temp3.dta

sort ID
merge ID using temp5.dta
drop if _m==2
drop _m
capture erase temp5.dta

sort ID
merge ID using temp4.dta
drop _m
capture erase temp4.dta

bys nace4_b,sort: egen avg_ind_sales=mean(sales_b)
replace sales_b/avg_ind_sales

foreach y of varlist indegree outdegree sales_b {
	replace `y'=ln(`y')
}

gen sdsales=sales
gen exporter=(exports!=0)
gen expintensity=2.7*exports/(domsalesvalue+2.7*exports)
gen importer=(imports!=0)
gen impintensity=2.7*imports/(dompurchvalue+wagebill_b+2.7*imports)

save temp3.dta,replace

collapse (sd) sdsales (mean) indegree outdegree supavgwage_uw supavgwage_w expintensity impintensity  (sum) exporter importer value_network (count) ID,by(wageq)

rename ID numfirm
gen shrexporter=exporter/numfirm
gen shrimporter=importer/numfirm

egen total_value_network=sum(value_network)
gen shrsales=value_network/total_value_network

drop exporter importer total_value_network value_network ID

foreach y of varlist supavgwage_uw supavgwage_w {
	replace `y'=`y'-`y'[1]
}


*Bootstrap

//Bootstrap (clustered)
qui{
forvalues i=1(1)100 {
use temp3.dta,clear
rename nace2_b Ind

bys Ind,sort: gen num=_N
local indsize=num
egen s=group(Ind)
su s
gen numind=r(max)
local numind2=numind
save temp4.dta,replace

forvalues j=1(1)`numind2'{
use temp4,clear
keep if s==`j' 
bsample ` indsize'
save bstrap`j',replace
}
use bstrap1,clear
for X in num 2/`numind2': append using bstrapX
sort avgwage_adj
xtile wageq = avgwage_adj, nq(5) 

collapse (sd) sdsales (mean) indegree outdegree supavgwage_uw supavgwage_w expintensity impintensity  (sum) exporter importer value_network (count) ID,by(wageq)

rename ID numfirm
gen shrexporter=exporter/numfirm
gen shrimporter=importer/numfirm

egen total_value_network=sum(value_network)
gen shrsales=value_network/total_value_network

drop exporter importer total_value_network value_network ID

foreach y of varlist supavgwage_uw supavgwage_w {
	replace `y'=`y'-`y'[1]
}

gen rep=`i'
save moments_boot`i'.dta,replace
}

use moments_boot1.dta
for X in num 2/100: append using  moments_bootX

for X in num 1/100: capture erase  moments_bootX
for X in num 1/`numind2': capture erase  bstrapX
}

capture erase temp3.dta
capture erase temp4.dta


/***************************************
/Table A7
****************************************/

*Load bootstrap data

local varlist "indegree outdegree sdsales shrsales shrexporter expintensity supavgwage_uw supavgwage_w"

foreach v in `varlist'{
	gen sd_`v'=`v'
	gen mean_`v'=`v'
}

collapse (sd) sd_* (mean) mean_*,by(wageq)


merge 1:1 wageq using "ModelvsData"

foreach v in `varlist'{
	gen lb_`v'=`v'_data-(sqrt((1+1/100)*(sd_`v'^2)))*1.96 // adjust by the number of simulations
	gen ub_`v'=`v'_data+(sqrt((1+1/100)*(sd_`v'^2)))*1.96
}

tw (scatter indegree_model wageq) (rcap lb_indegree ub_indegree wageq),graphregion(color(white)) legend(off) xtitle("Wage quintiles") title("Number of suppliers",size(medium)) saving(num_suppliers.gph,replace)

tw (scatter outdegree_model wageq) (rcap lb_outdegree ub_outdegree wageq),graphregion(color(white)) legend(off) xtitle("Wage quintiles") title("Number of customers",size(medium)) saving(num_buyers.gph,replace)

tw (scatter shrsales_model wageq) (rcap lb_shrsales ub_shrsales wageq),graphregion(color(white)) legend(off) xtitle("Wage quintiles") title("Share of total network sales",size(medium)) saving(shr_sales.gph,replace)

tw (scatter sdsales_model wageq) (rcap lb_sdsales ub_sdsales wageq),graphregion(color(white)) legend(off) xtitle("Wage quintiles") title("Standard deviation of log sales",size(medium)) saving(se_sales.gph,replace)

tw (scatter shrexporter_model wageq) (rcap lb_shrexporter ub_shrexporter wageq),graphregion(color(white)) legend(off) xtitle("Wage quintiles") title("Fraction of exporters",size(medium)) saving(frac_exporters.gph,replace)

tw (scatter expintensity_model wageq) (rcap lb_expintensity ub_expintensity wageq),graphregion(color(white)) legend(off) xtitle("Wage quintiles") title("Export intensity of exporters",size(medium)) saving(exp_intensity.gph,replace)

tw (scatter supavgwage_uw_model wageq) (rcap lb_supavgwage_uw ub_supavgwage_uw wageq),graphregion(color(white)) legend(off) xtitle("Wage quintiles") title("Uwgt. avg. log wage of suppliers",size(medium)) saving(uw_supwages.gph,replace)


tw (scatter supavgwage_w_model wageq) (rcap lb_supavgwage_w ub_supavgwage_w wageq),graphregion(color(white)) legend(off) xtitle("Wage quintiles") title("Wgt. avg. log wage of suppliers",size(medium)) saving(w_supwages.gph,replace)


gr combine num_suppliers.gph num_buyers.gph shr_sales.gph se_sales.gph  frac_exporters.gph exp_intensity.gph uw_supwages.gph w_supwages.gph,graphregion(color(white))
gr export CIs.png,replace

foreach v in `varlist'{
	capture erase `v'.gph
}
